package com.hooware.dao;

import com.hooware.domain.Role;
import com.hooware.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {

    @Select("select * from users where username = #{username}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(many = @Many(select = "com.hooware.dao.IRoleDao.findRoleByUserId"), column = "id", property = "roles")
    })
    UserInfo findByUsername(String username);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Insert("insert into users values(default, #{email}, #{username}, #{password}, #{phoneNum}, #{status})")
    void saveUser(UserInfo userInfo);

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(many = @Many(select = "com.hooware.dao.IRoleDao.findRoleByUserId"), column = "id", property = "roles")
    })
    UserInfo findById(String id);

    @Select("select * from role where id not in (select roleid from users_role where userid = #{id})")
    List<Role> findOtherRoles(String id);

//    @Insert("insert into users_role values(#{param1},#{param2})")
    @Insert("insert into users_role values(#{uid},#{rid})")
    void addRoleToUser(@Param("uid") String uid, @Param("rid") String rid);
}
